﻿using com.nm.library.Excels;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.nm.library;
using System.Data;
using System.Reflection;
using com.nm.db.Models.Terminal;
using com.nm;
using com.nm.Helper;
using com.nm.library.Helper;
using george.Helper;

namespace TerminalAPI.ImportExcel
{
    [Export(typeof(ExcelImport))]
    public class ShopExcel : ExcelImport
    {
        public override Dictionary<string, ImportVerify> DictFields => dictFields;

        /// <summary>
        /// Excel字段映射
        /// </summary>
        private static Dictionary<string, ImportVerify> dictFields = new List<ImportVerify> {
            new ImportVerify{ ColumnName="年份",FieldName="Year",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,6,false,true)},
            new ImportVerify{ ColumnName="季节",FieldName="Season",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,4,false,true)},
            new ImportVerify{ ColumnName="店铺级别",FieldName="ShopLevels",DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true) },
            new ImportVerify{ ColumnName="店铺陈列级别",FieldName="Levels",DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true) },
            new ImportVerify{ ColumnName="企划区域",FieldName="Area" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,10,false,false)},
            new ImportVerify{ ColumnName="店铺编号",        FieldName="ShopCode" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,30,true,false)},
            new ImportVerify{ ColumnName="店铺简称",    FieldName="ShopName" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,true,false)},
            new ImportVerify{ ColumnName="省份",  FieldName="Province" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,10,false,false)},
            new ImportVerify{ ColumnName="城市",  FieldName="City" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,10,false,false)},
            new ImportVerify{ ColumnName="隶属区域",    FieldName="SubordinateArea" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,10,false,false)},
            new ImportVerify{ ColumnName="营业状态",    FieldName="BusinessStatus" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,10,false,false)},
            new ImportVerify{ ColumnName="开业日期",    FieldName="OpeningDate" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true)},
            new ImportVerify{ ColumnName="关店日期",    FieldName="CloseDate" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true)},
            new ImportVerify{ ColumnName="停业日期",    FieldName="SuspensionDate" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true)},
            new ImportVerify{ ColumnName="复业日期",    FieldName="ResumptionDate" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true)},
            new ImportVerify{ ColumnName="经营模式",    FieldName="BusinessModel" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,10,false,true)},
            new ImportVerify{ ColumnName="营业面积",    FieldName="BusinessArea" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,10,false,true)},
            new ImportVerify{ ColumnName="仓库面积",    FieldName="WarehouseArea" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,10,false,true)},
            new ImportVerify{ ColumnName="物流时间天",   FieldName="LogisticsTimeDay" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,10,false,true)},
            new ImportVerify{ ColumnName="店铺形象",    FieldName="StoreImage" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,10,false,true)},
            new ImportVerify{ ColumnName="经营业态",    FieldName="OperatingFormat" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,10,false,true)},
            new ImportVerify{ ColumnName="商圈类型",    FieldName="BusinessDistrictType" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,10,false,true)},
            new ImportVerify{ ColumnName="商圈级别",    FieldName="BusinessDistrictLevel" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,10,false,true)},
            new ImportVerify{ ColumnName="学生消费群体",  FieldName="ConsumerGroupsOfStudent" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,false)},
            new ImportVerify{ ColumnName="蓝领消费群体",  FieldName="ConsumerGroupsOfBlueCollar" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,false)},
            new ImportVerify{ ColumnName="白领消费群体",  FieldName="ConsumerGroupsOfWhiteCollar" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,false)},
            new ImportVerify{ ColumnName="旅游消费群体",  FieldName="ConsumerGroupsOfVisitor" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,false)},
            new ImportVerify{ ColumnName="年龄层18-22",    FieldName="AgeGroup18To22" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,false)},
            new ImportVerify{ ColumnName="年龄层23-27",    FieldName="AgeGroup23To27" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,false)},
            new ImportVerify{ ColumnName="年龄层28-34",    FieldName="AgeGroup28To34" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,false)},
            new ImportVerify{ ColumnName="年龄层35-40",    FieldName="AgeGroup35To40" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,false)},
            new ImportVerify{ ColumnName="休闲系列",    FieldName="LeisureSeries" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,false)},
            new ImportVerify{ ColumnName="精英系列",    FieldName="EliteSeries" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,false)},
            new ImportVerify{ ColumnName="创意系列",    FieldName="CreativeSeries" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,false)},
            new ImportVerify{ ColumnName="款量需求",    FieldName="StyleNumber" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true)},
            new ImportVerify{ ColumnName="客群特征",    FieldName="FixedFlow" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true)},
            new ImportVerify{ ColumnName="特别备注",    FieldName="SpecialNote" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,200,false,true)},
           }.ToDictionary(e => e.ColumnName, e => e);

        public override ExcelImportType Type => ExcelImportType.SHOP;

        ///// <summary>
        /////返回对应的导出模版数据
        ///// </summary>
        ///// <param name="FilePath">模版的路径</param>
        ///// <param name="s">响应流</param>
        ///// <returns>模版MemoryStream</returns>
        //public override void GetExportTemplate(string FilePath, Stream s)
        //{
        //    //写入下拉框值 任务状态
        //    var sheet = NPOIHelper.GetFirstSheet(FilePath);

        //    string[] taskStatus = GetStatusDict().Keys.ToArray();

        //    int dataRowIndex = StartRowIndex + 1;
        //    NPOIHelper.SetHSSFValidation(sheet, taskStatus, dataRowIndex, 3);

        //    sheet.Workbook.Write(s);
        //}

        public override object SaveImportData(DataTable dt, Dictionary<string, object> extraInfo, int userId)
        {
            object test = 0;
            var index = dt.Rows.Count;
            var msg = $"成功导入{index}条数据";
            try
            {
                var dc = new TerminalEntities();
                foreach (DataRow item in dt.Rows)
                {
                    test = item["ShopCode"];
                    var shopCode = item["ShopCode"].ToString().ToUpper();
                    var query = dc.Tm_Shop.FirstOrDefault(t => t.ShopCode == shopCode);
                    if (query != null)
                    {
                        query.Operator = userId;
                        query.Levels = item["Levels"].ToString().Trim();
                        query.ShopCode = shopCode.Trim();
                        query.ModifyDate = DateTime.Now;
                        query.Year = item["Year"].ToString().Trim();
                        query.Season = item["Season"].ToString().Trim();
                        query.Levels = item["Levels"].ToString().Trim();
                        query.ShopLevels = item["ShopLevels"].ToString().Trim();
                        query.ShopName = item["ShopName"].ToString().Trim();
                        query.Province = item["Province"].ToString().Trim();
                        query.City = item["City"].ToString().Trim();
                        query.SubordinateArea = item["SubordinateArea"].ToString().Trim();
                        query.BusinessStatus = item["BusinessStatus"].ToString().Trim();
                        query.OpeningDate = item["OpeningDate"].ToString().Trim();
                        query.CloseDate = item["CloseDate"].ToString().Trim();
                        query.SuspensionDate = item["SuspensionDate"].ToString().Trim();
                        query.ResumptionDate = item["ResumptionDate"].ToString().Trim();
                        query.BusinessModel = item["BusinessModel"].ToString().Trim();
                        query.BusinessArea = item["BusinessArea"].ToString().Trim();
                        query.WarehouseArea = item["WarehouseArea"].ToString().Trim();
                        query.LogisticsTimeDay = item["LogisticsTimeDay"].ToString().Trim();
                        query.StoreImage = item["StoreImage"].ToString().Trim();
                        query.BusinessDistrictLevel = item["BusinessDistrictLevel"].ToString().Trim();
                        query.OperatingFormat = item["OperatingFormat"].ToString().Trim();
                        query.BusinessDistrictType = item["BusinessDistrictType"].ToString().Trim();
                        query.ConsumerGroupsOfStudent = item["ConsumerGroupsOfStudent"].ToString().Trim();
                        query.ConsumerGroupsOfBlueCollar = item["ConsumerGroupsOfBlueCollar"].ToString().Trim();
                        query.ConsumerGroupsOfWhiteCollar = item["ConsumerGroupsOfWhiteCollar"].ToString().Trim();
                        query.ConsumerGroupsOfVisitor = item["ConsumerGroupsOfVisitor"].ToString().Trim();
                        if (item["AgeGroup18To22"] != DBNull.Value)
                        {
                            decimal temp = 0;
                            decimal.TryParse(item["AgeGroup18To22"]?.ToString().Trim(), out temp);
                            query.AgeGroup18To22 = temp;
                        }
                        if (item["AgeGroup23To27"] != DBNull.Value)
                        {
                            decimal temp = 0;
                            decimal.TryParse(item["AgeGroup23To27"]?.ToString().Trim(), out temp);
                            query.AgeGroup23To27 = temp;
                        }
                        if (item["AgeGroup28To34"] != DBNull.Value)
                        {
                            decimal temp = 0;
                            decimal.TryParse(item["AgeGroup28To34"]?.ToString().Trim(), out temp);
                            query.AgeGroup28To34 = temp;
                        }
                        if (item["AgeGroup35To40"] != DBNull.Value)
                        {
                            decimal temp = 0;
                            decimal.TryParse(item["AgeGroup35To40"]?.ToString().Trim(), out temp);
                            query.AgeGroup35To40 = temp;
                        }
                        if (item["LeisureSeries"] != DBNull.Value)
                        {
                            decimal temp = 0;
                            decimal.TryParse(item["LeisureSeries"]?.ToString().Trim(), out temp);
                            query.LeisureSeries = temp;
                        }
                        if (item["EliteSeries"] != DBNull.Value)
                        {
                            decimal temp = 0;
                            decimal.TryParse(item["EliteSeries"]?.ToString().Trim(), out temp);
                            query.EliteSeries = temp;
                        }
                        if (item["CreativeSeries"] != DBNull.Value)
                        {
                            decimal temp = 0;
                            decimal.TryParse(item["CreativeSeries"]?.ToString().Trim(), out temp);
                            query.CreativeSeries = temp;
                        }
                        query.StyleNumber = item["StyleNumber"].ToString().Trim();
                        query.FixedFlow = item["FixedFlow"].ToString().Trim();
                        query.SpecialNote = item["SpecialNote"].ToString().Trim();
                    }
                    else
                    {
                        decimal AgeGroup18To22 = 0;
                        decimal AgeGroup23To27 = 0;
                        decimal AgeGroup28To34 = 0;
                        decimal AgeGroup35To40 = 0;
                        decimal LeisureSeries = 0;
                        decimal EliteSeries = 0;
                        decimal CreativeSeries = 0;
                        if (item["AgeGroup18To22"] != DBNull.Value)
                        {
                            decimal temp = 0;
                            decimal.TryParse(item["AgeGroup18To22"]?.ToString().Trim(), out temp);
                            AgeGroup18To22 = temp;
                        }
                        if (item["AgeGroup23To27"] != DBNull.Value)
                        {
                            decimal temp = 0;
                            decimal.TryParse(item["AgeGroup23To27"]?.ToString().Trim(), out temp);
                            AgeGroup23To27 = temp;
                        }
                        if (item["AgeGroup28To34"] != DBNull.Value)
                        {
                            decimal temp = 0;
                            decimal.TryParse(item["AgeGroup28To34"]?.ToString().Trim(), out temp);
                            AgeGroup28To34 = temp;
                        }
                        if (item["AgeGroup35To40"] != DBNull.Value)
                        {
                            decimal temp = 0;
                            decimal.TryParse(item["AgeGroup35To40"]?.ToString().Trim(), out temp);
                            AgeGroup35To40 = temp;
                        }
                        if (item["LeisureSeries"] != DBNull.Value)
                        {
                            decimal temp = 0;
                            decimal.TryParse(item["LeisureSeries"]?.ToString().Trim(), out temp);
                            LeisureSeries = temp;
                        }
                        if (item["EliteSeries"] != DBNull.Value)
                        {
                            decimal temp = 0;
                            decimal.TryParse(item["EliteSeries"]?.ToString().Trim(), out temp);
                            EliteSeries = temp;
                        }
                        if (item["CreativeSeries"] != DBNull.Value)
                        {
                            decimal temp = 0;
                            decimal.TryParse(item["CreativeSeries"]?.ToString().Trim(), out temp);
                            CreativeSeries = temp;
                        }
                        var shop = new Tm_Shop
                        {
                            CreateDate = DateTime.Now,
                            ShopCode = shopCode.Trim(),
                            IsRemove = false,
                            ModifyDate = DateTime.Now,
                            Operator = userId,
                            Year = item["Year"].ToString().Trim(),
                            Season = item["Season"].ToString().Trim(),
                            Levels = item["Levels"].ToString().Trim(),
                            ShopLevels = item["ShopLevels"].ToString().Trim(),
                            ShopName = item["ShopName"].ToString().Trim(),
                            Province = item["Province"].ToString().Trim(),
                            City = item["City"].ToString().Trim(),
                            SubordinateArea = item["SubordinateArea"].ToString().Trim(),
                            BusinessStatus = item["BusinessStatus"].ToString().Trim(),
                            OpeningDate = item["OpeningDate"].ToString().Trim(),
                            CloseDate = item["CloseDate"].ToString().Trim(),
                            SuspensionDate = item["SuspensionDate"].ToString().Trim(),
                            ResumptionDate = item["ResumptionDate"].ToString().Trim(),
                            BusinessModel = item["BusinessModel"].ToString().Trim(),
                            BusinessArea = item["BusinessArea"].ToString().Trim(),
                            WarehouseArea = item["WarehouseArea"].ToString().Trim(),
                            LogisticsTimeDay = item["LogisticsTimeDay"].ToString().Trim(),
                            StoreImage = item["StoreImage"].ToString().Trim(),
                            BusinessDistrictLevel = item["BusinessDistrictLevel"].ToString().Trim(),
                            OperatingFormat = item["OperatingFormat"].ToString().Trim(),
                            BusinessDistrictType = item["BusinessDistrictType"].ToString().Trim(),
                            ConsumerGroupsOfStudent = item["ConsumerGroupsOfStudent"].ToString().Trim(),
                            ConsumerGroupsOfBlueCollar = item["ConsumerGroupsOfBlueCollar"].ToString().Trim(),
                            ConsumerGroupsOfWhiteCollar = item["ConsumerGroupsOfWhiteCollar"].ToString().Trim(),
                            ConsumerGroupsOfVisitor = item["ConsumerGroupsOfVisitor"].ToString().Trim(),
                            AgeGroup18To22 = AgeGroup18To22,
                            AgeGroup23To27 = AgeGroup23To27,
                            AgeGroup28To34 = AgeGroup28To34,
                            AgeGroup35To40 = AgeGroup35To40,
                            LeisureSeries = LeisureSeries,
                            EliteSeries = EliteSeries,
                            CreativeSeries = CreativeSeries,
                            StyleNumber = item["StyleNumber"].ToString().Trim(),
                            FixedFlow = item["FixedFlow"].ToString().Trim(),
                            SpecialNote = item["SpecialNote"].ToString().Trim(),
                        };
                        dc.Tm_Shop.Add(shop);
                    }
                }
                dc.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return new ImportResult()
                {
                    IsSuccess = false,
                    ExtraInfo = ex,
                    Message = ex.Message
                };
            }
            return new ImportResult()
            {
                IsSuccess = true,
                ExtraInfo = "导入成功",
                Message = msg
            };
        }

        /// <summary>
        /// 获取表格数据
        /// </summary>
        /// <returns></returns>
        public override DataTable GetExportData(string Keys)
        {
            var dc = new TerminalEntities();
            var query = (from shop in dc.vm_exportshop
                         select shop).ToList().Select(o => new
                         {
                             o.Area,
                             o.ShopCode,
                             o.ShopLevels,
                             o.ShopName,
                             o.sid,
                             //Image = ImageHelper.GetResolveImageUrl(o.ShopCode, "shop", "http")
                         });
            return DataTableHelper.LinqToDataTable(query);
        }
    }
}
